package com.mjk.common.web.getway.conf;

import com.mjk.common.web.getway.restful.prototal.http.GetWayHttpIntercept;
import jakarta.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 网关配置
 * @author mjk
 *
 */
@Configuration
public class GetWayConf implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 对所有接口都应用跨域配置
                .allowedOrigins("*") // 允许任何域名
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(false)
                .maxAge(3600); // 缓存有效期
    }

	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxRequestSize(DataSize.parse("200MB"));
        factory.setMaxFileSize(DataSize.parse("200MB"));
	    return factory.createMultipartConfig();
	}

	
	@Bean
	public GetWayHttpIntercept getGetWayInterceptor(){
		return new GetWayHttpIntercept();
	}
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {		
		registry.addInterceptor(getGetWayInterceptor()).addPathPatterns("/ld/**")
		.excludePathPatterns("/ld/ws/*");				
		//super.addInterceptors(registry);
	}
	@Override
	public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(10);
		taskExecutor.setMaxPoolSize(100);
		taskExecutor.setThreadNamePrefix("async-task-");
		taskExecutor.initialize();

		configurer.setTaskExecutor(taskExecutor);
	}
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/plumelog/**").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/amis/**").addResourceLocations(
				"classpath:/amis/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        //static 目录下文件不缓存
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/").setCacheControl(CacheControl.noStore());
       // super.addResourceHandlers(registry);
	}
}
